package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.b1;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class SubscribeParam {
  private byte[] src;
  private List<SubscribeInfo> subscribeInfoList;
  //uuid,64个字节
  private byte[] uuid;

  public SubscribeParam(List<SubscribeInfo> subscribeInfoList) {
    this.subscribeInfoList = subscribeInfoList;
    this.uuid = generate64ByteUUID();
  }

  public byte[] getUuid() {
    return uuid;
  }

  // 生成 64 字节长度的 UUID，返回 byte[] 类型
  public static byte[] generate64ByteUUID() {
    // 创建 SecureRandom 实例用于生成安全的随机数
    SecureRandom secureRandom = new SecureRandom();
    // 定义一个长度为 64 的字节数组
    byte[] uuidBytes = new byte[64];
    // 使用 SecureRandom 生成随机字节填充字节数组
    secureRandom.nextBytes(uuidBytes);
    return uuidBytes;
  }

  public byte[] toBytes() {
    src = new byte[192];
    List<Byte> bytes = new ArrayList<>();
    for (SubscribeInfo subscribeInfo : subscribeInfoList) {
      bytes.add(subscribeInfo.commandCode[0]);
      bytes.add(subscribeInfo.commandCode[1]);
      bytes.add(subscribeInfo.intervalTime[0]);
      bytes.add(subscribeInfo.intervalTime[1]);
      bytes.add(subscribeInfo.durationTime[0]);
      bytes.add(subscribeInfo.durationTime[1]);
      bytes.add(subscribeInfo.durationTime[2]);
      bytes.add(subscribeInfo.durationTime[3]);
      bytes.add(subscribeInfo.reserved[0]);
      bytes.add(subscribeInfo.reserved[1]);
      bytes.add(subscribeInfo.reserved[2]);
      bytes.add(subscribeInfo.reserved[3]);
      bytes.add(subscribeInfo.reserved[4]);
      bytes.add(subscribeInfo.reserved[5]);
      bytes.add(subscribeInfo.reserved[6]);
      bytes.add(subscribeInfo.reserved[7]);
    }

    for (int i = 0; i < bytes.size(); i++) {
      src[i] = bytes.get(i);
    }
    for (int i = 0; i < uuid.length; i++) {
      src[i + 128] = uuid[i];
    }

    return src;
  }


}
